{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Face++面部识别+人脸相似度\n",
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "# 2、输入我们API_Key/API_secret\n",
    "api_key = '7R76VsrGkLJxzkW1hAqQefd4a_bFR36S'\n",
    "\n",
    "api_secret = 'Aia6oxeHYD2Mvxx1sncTOpCd7bIH4Hh-'\n",
    "\n",
    "# 3、目标url\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'  # 请求URL 即平台\n",
    "img_url1 = 'https://gitee.com/li_man_qi/api_start/raw/master/images/dl.jpg'\n",
    "\n",
    "# 4、头部headers\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、调用参数\n",
    "payload = {\n",
    "    \"image_url\":img_url1,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603536791,ecdd655f-fdac-497f-896e-cc910bba6ebf\",\"time_used\":622,\"faces\":[{\"face_token\":\"2b52f1d6e17ef03ed9e2fa57b217e39a\",\"face_rectangle\":{\"top\":412,\"left\":471,\"width\":333,\"height\":333},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":29},\"smile\":{\"value\":99.792,\"threshold\":50.000},\"emotion\":{\"anger\":0.196,\"disgust\":1.696,\"fear\":0.196,\"happiness\":35.453,\"neutral\":0.623,\"sadness\":55.003,\"surprise\":6.833}}}],\"image_id\":\"uoanrZIZzYYjtGTJclvB0g==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603536791,ecdd655f-fdac-497f-896e-cc910bba6ebf',\n",
       " 'time_used': 622,\n",
       " 'faces': [{'face_token': '2b52f1d6e17ef03ed9e2fa57b217e39a',\n",
       "   'face_rectangle': {'top': 412, 'left': 471, 'width': 333, 'height': 333},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 29},\n",
       "    'smile': {'value': 99.792, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.196,\n",
       "     'disgust': 1.696,\n",
       "     'fear': 0.196,\n",
       "     'happiness': 35.453,\n",
       "     'neutral': 0.623,\n",
       "     'sadness': 55.003,\n",
       "     'surprise': 6.833}}}],\n",
       " 'image_id': 'uoanrZIZzYYjtGTJclvB0g==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#df = pd.json_normalize(results,record_path='faces')\n",
    "#pandas数据清洗失败 哭死在厕所了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸相似度对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = 'Aia6oxeHYD2Mvxx1sncTOpCd7bIH4Hh-'\n",
    "api_key =  '7R76VsrGkLJxzkW1hAqQefd4a_bFR36S'  # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FaceSet Create（创建人脸集合）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"朕的后宫\"\n",
    "outer_id = \"supermonkey03\"\n",
    "user_data = \"5人，邓伦，张新成，李明德，郑业成，成毅，\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url, params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'f413dd3a2856f876733400b8a7c6587a',\n",
       " 'time_used': 153,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603536807,4116ebab-2d3f-4589-9ce0-57ad7d48b269',\n",
       " 'outer_id': 'supermonkey03',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FaceSet GetDetail（获取人脸集合信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'f413dd3a2856f876733400b8a7c6587a',\n",
       " 'tags': '',\n",
       " 'time_used': 90,\n",
       " 'user_data': '5人，邓伦，张新成，李明德，郑业成，成毅，',\n",
       " 'display_name': '朕的后宫',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603536810,15a28146-224e-4f4d-b5d2-393cf9505e9b',\n",
       " 'outer_id': 'supermonkey03'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FaceSet AddFace（增加人脸信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'fa760ca327ed11563860e94e3391f7ef', #要和前面的faceset token一致\n",
    "    'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa760ca327ed11563860e94e3391f7ef',\n",
       " 'time_used': 69,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603536812,7a124f31-3a73-4a1b-a002-ecc83e54c7ed',\n",
       " 'outer_id': 'supermonkey02',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'b0407b9e803ebd39d511cd7956fd5bf5'}]}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FaceSet RemoveFace（移除人脸信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'fa760ca327ed11563860e94e3391f7ef',\n",
    "    'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa760ca327ed11563860e94e3391f7ef',\n",
       " 'face_removed': 0,\n",
       " 'time_used': 140,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603536814,49791a36-7543-4446-ba44-2ee7f268ffde',\n",
       " 'outer_id': 'supermonkey02',\n",
       " 'failure_detail': [{'reason': 'FACE_NOT_IN_FACESET',\n",
       "   'face_token': 'b0407b9e803ebd39d511cd7956fd5bf5'}]}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FaceSet Update（更新人脸集合信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'fa760ca327ed11563860e94e3391f7ef',\n",
    "    'user_data':\"5人，邓伦，张新成，李明德，郑业成，成毅，\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa760ca327ed11563860e94e3391f7ef',\n",
       " 'request_id': '1603536817,57713e4d-667d-4960-bc1c-50759e074461',\n",
       " 'time_used': 73,\n",
       " 'outer_id': 'supermonkey02'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compare Face（对比人脸相似度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "dl3 = \"https://gitee.com/li_man_qi/api_start/raw/master/images/dl3.jpg\"\n",
    "zxc = \"https://gitee.com/li_man_qi/api_start/raw/master/images/zxc.jpg\"\n",
    "lmd = \"https://gitee.com/li_man_qi/api_start/raw/master/images/lmd.jpg\"\n",
    "zyc = \"https://gitee.com/li_man_qi/api_start/raw/master/images/zyc.jpg\"\n",
    "cy = \"https://gitee.com/li_man_qi/api_start/raw/master/images/cy.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':dl3,\n",
    "    'image_url2':zxc,\n",
    "    'image_url3':lmd,\n",
    "    'image_url3':zyc,\n",
    "    'image_url3':cy,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 385,\n",
       "    'top': 347,\n",
       "    'left': 564,\n",
       "    'height': 385},\n",
       "   'face_token': '71ab81ed6bffe932ba67ac512a166aa3'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 116,\n",
       "    'top': 205,\n",
       "    'left': 150,\n",
       "    'height': 116},\n",
       "   'face_token': '5a9fb8dd995ece004982f13aab3c3760'}],\n",
       " 'time_used': 1458,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 70.811,\n",
       " 'image_id2': 'OiYH90wEg/PvlqmhfInJzQ==',\n",
       " 'image_id1': '9xGd0WILw8/k53CrSP6/+Q==',\n",
       " 'request_id': '1603536850,56440c49-a025-4910-9b1d-4c974baec38b'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
